const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        Member_SerialNumber: [{ required: true, message: '请输入会员编号', trigger: 'blur' }],
        Nick: [{ required: true, message: '请输入会员昵称', trigger: 'blur' }],
        Member_Name: [{ required: true, message: '请输入会员名称', trigger: 'blur' }],
        Mobile: [{ required: true, message: '请输入电话号', trigger: 'blur' }],
        Open_ID: [{ required: true, message: '请输入消费客户编号', trigger: 'blur' }],
        Sales_ID: [{ required: true, message: '请输入销售人员编号', trigger: 'blur' }],
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        Member_SerialNumber: '',
        Nick: '',
        Member_Name: '',
        Mobile: '',
        Head_URL: '',
        Open_ID: '',
        Sales_ID: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          member_account_info: [{
            Member_Account_SerialNumber: '',
            Member_SerialNumber: '',  //会员账户编号
            Account_Type: '', //账户类型
            Prepayment: '',   //预付款
            Prepayment_Sales: '',
            Prepayment_Balance: '',
            Gift_Amount: '',  //赠送金额
            Gift_Sales: '',
            Gift_Balance: '',
            Sales_Point: '',   //消费积分
            Sales_Point_Sales: '',
            Sales_Point_Balance: '',
            Gift_Point: '',    //赠送积分
            Gift_Point_Sales: '',
            Gift_Point_Balance: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }]
        }
      },
      loading: false
    }
  },
  mounted() {

  },
  methods: {
    submitForm(formName) {
      let _this = this
      if (_this.form.Children.member_account_info[0].Account_Type == '' || _this.form.Children.member_account_info[0].Prepayment == '' || _this.form.Children.member_account_info[0].Gift_Amount == '' || _this.form.Children.member_account_info[0].Sales_Point == '' || _this.form.Children.member_account_info[0].Gift_Point == '' || _this.form.Children.member_account_info[0].Member_SerialNumber == '') {
        return _this.$message.error("请完善信息")
      }
      console.log(_this.form)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: '../addGeneralBusinessAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                enterprise_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './Member.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_barcode() {
      this.form.Children.barcode.push({       //条形码
        BarCode: "",
        Article_SerialNumber: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
      console.log(this.form.Children.barcode);
    },
    remove_barcode() {
      if (this.form.Children.barcode.length > 1) {
        this.form.Children.barcode.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_purchase_unitprice() {
      this.form.Children.purchase_unitprice.push({     //采购价格单
        Purchase_unitprice_SerialNumber: "",
        Start_Date: '',
        End_Date: '',
        Supplier_SerialNumber: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_purchase_unitprice() {
      if (this.form.Children.purchase_unitprice.length > 1) {
        this.form.Children.purchase_unitprice.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_series() {
      this.form.Children.series.push({     //商品类型
        Series_SerialNumber: "",
        Series_Name: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_series() {
      if (this.form.Children.series.length > 1) {
        this.form.Children.series.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    // uploadFront(file) {
    //   let _this = this
    //   new Compressor(file, {
    //     quality: 0.05,
    //     // The compression process is asynchronous,
    //     // which means you have to access the `result` in the `success` hook function.
    //     success(file) {
    //       const formData = new FormData();
    //       formData.append('file', file, file.name);
    //       let oFileReader = new FileReader();
    //       oFileReader.readAsDataURL(file);
    //       oFileReader.onload = function (e) {
    //         _this.form.Children.employee_id[0].ID_Front = e.target.result
    //         $.ajax({
    //           type: 'POST',
    //           // url: "../upLoadMaterialAction",
    //           url: '../parseIDCardAction',
    //           data: {
    //             Data: e.target.result,
    //             DataLength: file.size,
    //             FileName: file.name
    //           },
    //           complete: function (XMLHttpRequest) {
    //             if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
    //               window.parent.document.location.href = XMLHttpRequest.getResponseHeader(
    //                 "CONTENTPATH");
    //             }
    //           },
    //           success: function (Result) {
    //             if (Result.Status == "success") {
    //               _this.form.Employee_Name = Result.name;
    //               _this.form.ID_Number = Result.ID_Number;
    //               _this.form.Gender = Result.gender;
    //               _this.form.Residence_Address = Result.address;
    //               _this.form.Nation = Result.nation;

    //             } else {
    //               _this.$message.error("身份证正面照片有误,请重新上传")
    //             }
    //           },
    //           dataType: "json"
    //         });
    //       }
    //     },
    //     error(err) {
    //       console.log(err.message);
    //     },
    //   });
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
})